public class test2 {
    public static void main(String[] args) throws InterruptedException
    {
        //创建每个线程的结果数组和线程数组
        int[] results = new int[10];
        Thread[] threads = new Thread[10];

        //接下来要创建并启动十个线程
        for(int i=0;i<=9;i++)
        {
            final int threadID = i;
            final int start = i*10+1; //起始数字
            final int end = start+9; //结束数字

            threads[threadID] = new Thread()
            {
                @Override
                public void run()
                {
                    int sum = 0;
                    for(int j=start;j<=end;j++)
                    {
                        sum += j;

                    }
                    results[threadID] = sum;
                    System.out.println("线程" + (threadID+1) + "计算结果:" + sum);
                }
            };

            threads[threadID].start();
        }

        //等所有线程执行完
        for(Thread e : threads)
        {
            e.join();
        }

        int totalsum = 0;
        //计算总结果
        for(int e : results)
        {
            totalsum += e;
        }
        System.out.println("所有线程结果总和: " + totalsum);
    }
}
